/* eslint-disable object-curly-newline */

/** 存放调用接口获取的数据 */
import moment from 'moment';
import { message } from 'antd';
import * as config from '../../utils/config';
import * as commonServices from '../../services/services';
import * as commonUtils from '../../utils/utils'; /* 通用方法 */

const dateFormat = 'YYYY-MM-DD HH:mm:ss'; /* 日期控件显示格式 */

/** 应收款，到期应收 */
export async function getOutData(props) {
  const { formId, masterData, isMasterRender } = props;
  const { sCustomerId } = props.masterData;
  const { token } = props.app;
  if (commonUtils.isNotEmptyStr(sCustomerId)) {
    const tStartDate = moment().subtract(1, 'days').format(dateFormat); /* 前一天 */
    const tEndDate = moment().add(1, 'days').format(dateFormat); /* 后一天 */
    const paramsMap = { iType: '0', sStatistics: sCustomerId, tStartDate, tEndDate };
    const value = { sProName: 'Sp_Receivables_AR', paramsMap };
    const url = `${config.server_host}business/getProData?sModelsId=${formId}`;
    const { data } = await commonServices.postValueService(token, value, url);
    if (data.code === 1) {
      const outDataRetrun = data.dataset.rows[0].dataSet.outData;
      if (commonUtils.isNotEmptyArr(outDataRetrun)) {
        masterData.dAccountReceivable = outDataRetrun[0].dNoReceiptMoney; /* 应收款 */
        masterData.dExpireReceivable = outDataRetrun[0].dDueReceiptMoney; /* 到期应收 */
      }
    } else {
      message.error(data.msg);
    }
  }
  const showRender = ['dAccountReceivable', 'dExpireReceivable'];
  return { masterData, showRender, isMasterRender };
}

/** 作废，取消作废 */
export async function handleOkInvalid(props) {
  const { sId, sModelsId, masterData, masterConfig, handleType } = props;
  const { token } = props.app;
  const { sTbName } = masterConfig;
  const value = {
    sClientType: '1',
    sId: [sId],
    sTableName: sTbName,
    handleType,
    sBooleanCheck: false,
  };
  const url = `${config.server_host}checkModel/updatebInvalid?sModelsId=${sModelsId}`;
  const { data } = await commonServices.postValueService(token, value, url);
  if (data.code === 1) {
    message.success(data.msg);
    masterData.bInvalid = handleType === 'toVoid'; /* 作废状态 */
  } else {
    message.error(data.msg);
  }
  return props;
}

